const api = require('../../utils/api');
const regeneratorRuntime = require('../../utils/runtime.js');
const filterDefaultActivity = {
  category: [],
  location: "",
  activity_status: "",
}
const filterDefault = {
  category: [],
  level: "",
  activity_status: "",
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // ys
    competitionFilters: {
      "category": ["商科", "理工科", "语言", "创新创业", "文体", "综合"],
      "level": ["校级", "市级", "省级", "国家级", "世界级"],
      "activity_status": ["未开始", "报名中", "已截止"],
    },
    activityFilters: {
      "category": ["志愿者", "讲座", "其他"],
      "location": ["线上", "线下"],
      "activity_status": ["未开始", "报名中", "已截止"],
    },
    title: "",
    filter: {},
    filterActivity: {},
    orderList: ["本周最热", "本月最热", "最新发布"],
    orderByActivity: "2",
    showOrderSectionActivity: false,
    showFilterSectionActivity: false,
    orderBy: "2",
    showOrderSection: false,
    showFilterSection: false,
    ysTypes: ["竞赛", "活动"],
    ysType: "竞赛",
    joinAreas: ["本校", "全国"],
    joinArea: "本校",
    joinWays: ["组队", "非组队", "不限"],
    joinWay: "不限",
    posts: [],
    postsActivity: [],
    page: 1,
    pageActivity: 1,
    nodata: false,
    nodataActivity: false,
    nomore: false,
    nomoreActivity: false,
    defaultSearchValue: "",
  },
  // competition
  onShowOrder(e) {
    if (this.data.ysType == "竞赛") {
      this.setData({
        showOrderSection: !this.data.showOrderSection,
        showFilterSection: false,
        showOrderColor: !this.data.showOrderColor,
        showFilterColor: false
      })
    } else {
      this.setData({
        showOrderSectionActivity: !this.data.showOrderSectionActivity,
        showFilterSectionActivity: false,
        showOrderColor: !this.data.showOrderColor,
        showFilterColor: false
      })
    }
  },
  onShowFilter(e) {
    if (this.data.ysType == "竞赛") {
      this.setData({
        showFilterSection: !this.data.showFilterSection,
        showOrderSection: false,
        showFilterColor: !this.data.showFilterColor,
        showOrderColor: false
      })
    } else {
      this.setData({
        showFilterSectionActivity: !this.data.showFilterSectionActivity,
        showOrderSectionActivity: false,
        showFilterColor: !this.data.showFilterColor,
        showOrderColor: false,
      })
    }
  },
  onFilterSubmit(e) {
    this.setData({
      showFilterSection: false,
      nomore: false,
      nodata: false,
      page: 1,
      posts: [],
    });
    this.getCompetitionList()
  },
  onFilterReset(e) {
    this.setData({
      filter: Object.assign({}, filterDefault),
      showFilterSection: false,
      nomore: false,
      nodata: false,
      page: 1,
      posts: [],
    });
    this.getCompetitionList()
  },
  onChangeOrder(e) {
    let orderBy = e.currentTarget.dataset.value;
    this.setData({
      orderBy: orderBy,
      showOrderSection: false,
      nomore: false,
      nodata: false,
      page: 1,
      posts: [],
    });
    this.getCompetitionList()
  },
  onChangeFilter(e) {
    let field = e.currentTarget.dataset.field;
    let udata = {}
    udata[`filter.${field}`] = e.detail
    this.setData(udata);
  },
  getCompetitionList: async function () {
    wx.showLoading({
      title: '加载中...',
    })
    let page = this.data.page
    if (this.data.nomore) {
      wx.hideLoading()
      return
    }
    // filter
    let filter = Object.assign({}, this.data.filter)
    filter.join_area = this.data.joinArea
    filter.join_way = this.data.joinWay
    filter.title = this.data.title
    let orderBy = this.data.orderBy
    let ysType = this.data.ysType
    let result = await api.getActivityList(page, filter, orderBy, ysType)
    if (result.data.list.length === 0) {
      this.setData({
        nomore: true
      })
      if (page === 1) {
        this.setData({
          nodata: true
        })
      }
    }
    else {
      this.setData({
        page: page + 1,
        posts: this.data.posts.concat(result.data.list),
      })
    }
    wx.hideLoading()
  },
  // activity
  onShowOrderActivity(e) {
    this.setData({
      showOrderSectionActivity: !this.data.showOrderSectionActivity,
      showFilterSectionActivity: false,
    })
  },
  onShowFilterActivity(e) {
    this.setData({
      showFilterSectionActivity: !this.data.showFilterSectionActivity,
      showOrderSectionActivity: false,
    })
  },
  onFilterSubmitActivity(e) {
    this.setData({
      showFilterSectionActivity: false,
      nomoreActivity: false,
      nodataActivity: false,
      pageActivity: 1,
      postsActivity: [],
    });
    this.getActivityList()
  },
  onFilterResetActivity(e) {
    this.setData({
      filterActivity: Object.assign({}, filterDefaultActivity),
      showFilterSectionActivity: false,
      nomoreActivity: false,
      nodataActivity: false,
      pageActivity: 1,
      postsActivity: [],
    });
    this.getActivityList()
  },
  onChangeOrderActivity(e) {
    let orderBy = e.currentTarget.dataset.value;
    this.setData({
      orderByActivity: orderByActivity,
      showOrderSectionActivity: false,
      nomoreActivity: false,
      nodataActivity: false,
      pageActivity: 1,
      postsActivity: [],
    });
    this.getActivityList()
  },
  onChangeFilterActivity(e) {
    let field = e.currentTarget.dataset.field;
    let udata = {}
    udata[`filter.${field}`] = e.detail
    this.setData(udata);
  },
  getActivityList: async function () {
    wx.showLoading({
      title: '加载中...',
    })
    let page = this.data.pageActivity
    if (this.data.nomoreActivity) {
      wx.hideLoading()
      return
    }
    // filter
    let filter = Object.assign({}, this.data.filterActivity)
    filter.join_area = this.data.joinArea
    filter.join_way = this.data.joinWay
    filter.title = this.data.title

    let orderBy = this.data.orderByActivity
    let ysType = this.data.ysType
    let result = await api.getActivityList(page, filter, orderBy, ysType)
    if (result.data.list.length === 0) {
      this.setData({
        nomoreActivity: true
      })
      if (page === 1) {
        this.setData({
          nodataActivity: true
        })
      }
    }
    else {
      this.setData({
        pageActivity: page + 1,
        postsActivity: this.data.postsActivity.concat(result.data.list),
      })
    }
    wx.hideLoading()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    const { ysType } = options
    if (!ysType) {
      await this.getCompetitionList()
    } else {
      this.setData({
        ysType: ysType,
      })
      if (ysType == "竞赛") {
        await this.getCompetitionList()
      } else {
        await this.getActivityList()
      }
    }
    this.setData({
      filter: Object.assign({}, filterDefault),
      filterActivity: Object.assign({}, filterDefaultActivity)
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: async function () {
    let that = this;
    let page = 1
    that.setData({
      page: page,
      posts: [],
      filter: "",
      nomore: false,
      nodata: false,
      defaultSearchValue: ""
    })
    await this.getActivityList("")
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    let whereItem = this.data.whereItem
    let filter = this.data.filter
    await this.getActivityList(whereItem[0], whereItem[1], whereItem[2])
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  /**
   * 点击文章明细
   */
  bindPostDetail: function (e) {
    let blogId = e.currentTarget.id;
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/detail/detail?type=' + type + '&id=' + blogId
    })
  },

  bindPostDetailActivity: function (e) {
    let blogId = e.currentTarget.id;
    wx.navigateTo({
      url: './activity/activity?id=' + blogId
    })
  },
  /**
   * 搜索功能
   * @param {} e 
   */
  bindconfirm: async function (e) {
    let that = this;
    // console.log('e.detail.value', e.detail.value)
    let page = 1
    that.setData({
      page: page,
      pageActivity: page,
      posts: [],
      postsActivity: [],
      title: e.detail.value,
      nomore: false,
      nomoreActivity: false,
      nodata: false,
      nodataActivity: false,
    })
    if (this.data.ysType === "活动") {
      await this.getActivityList()
    } else {
      await this.getCompetitionList()
    }
  },


  /**
 * 参赛方式-tab切换
 * @param {} e 
 */
  tagSelectJoinWay: async function (e) {
    let that = this;
    let joinWay = e.currentTarget.dataset.value
    if (this.data.ysType == "竞赛") {
      that.setData({
        joinWay: joinWay,
        // "filter.join_way": tagCur,
        nomore: false,
        nodata: false,
        defaultSearchValue: "",
        posts: [],
        page: 1,
      })
      await that.getCompetitionList()
    } else {
      that.setData({
        joinWay: joinWay,
        // "filter.join_way": tagCur,
        nomoreActivity: false,
        nodataActivity: false,
        defaultSearchValue: "",
        postsActivity: [],
        pageActivity: 1,
      })
      await that.getActivityList()
    }

  },

  /**
 * 参赛地区-tab切换
 * @param {} e 
 */
  tagSelect: async function (e) {
    let that = this;
    let tagCur = e.currentTarget.dataset.value
    if (this.data.ysType == "活动") {
      that.setData({
        joinArea: tagCur,
        nomoreActivity: false,
        nodataActivity: false,
        defaultSearchValue: "",
        postsActivity: [],
        pageActivity: 1,
      })
      await that.getActivityList()
    } else {
      that.setData({
        joinArea: tagCur,
        nomore: false,
        nodata: false,
        defaultSearchValue: "",
        posts: [],
        page: 1,
      })
      await that.getCompetitionList()
    }
  },

  /**
   * 易赛类型-tab切换
   * @param {} e 
   */
  tabSelect: async function (e) {
    let that = this;
    let ysType = e.currentTarget.dataset.value
    that.setData({
      ysType: ysType,
      defaultSearchValue: "",
    })
    await that.getActivityList()
  },

  /**
   * 热门按钮切换
   * @param {*} e 
   */
  hotSelect: async function (e) {
    let that = this
    let hotCur = e.currentTarget.dataset.id
    let orderBy = "createTime"
    switch (hotCur) {
      //浏览最多
      case 0: {
        orderBy = "totalVisits"
        break
      }
      //评论最多
      case 1: {
        orderBy = "totalComments"
        break
      }
      //点赞最多
      case 2: {
        orderBy = "totalZans"
        break
      }
      //收藏最多
      case 3: {
        orderBy = "totalCollection"
        break
      }
    }
    that.setData({
      posts: [],
      hotCur: hotCur,
      defaultSearchValue: "",
      page: 1,
      nomore: false,
      nodata: false,
      whereItem: ['', orderBy, '']
    })
    await that.getActivityList("", orderBy)
  },

  /**
   * 标签按钮切换
   * @param {*} e 
   */
  labelSelect: async function (e) {
    let that = this
    let labelCur = e.currentTarget.dataset.id

    that.setData({
      posts: [],
      labelCur: labelCur,
      defaultSearchValue: "",
      page: 1,
      nomore: false,
      nodata: false,
      whereItem: ['', 'createTime', labelCur == "全部" ? "" : labelCur]
    })

    await that.getActivityList("", "createTime", labelCur == "全部" ? "" : labelCur)
  }
})